from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from chat.models import *
from user.models import UserModel
from shops.models import ShopsModel
from django.db.models import Q
# Create your views here.



class UserSpeak(APIView):
    def get(self,request):
        bid = request.query_params.get('shops_id')
        uid = request.query_params.get('user_id')
        print("uid?",uid,"bid???",bid)
        data = []
        userspk = Speaking.objects.filter(bid_id=bid,uid_id=uid).first()
        print(">>>>>>>>",userspk)
        chatid = userspk.chatid

        speakdata = Speaking.objects.filter(chatid=chatid).first()
        print("ZZZZZZZZZZZZ",speakdata)
        user = UserModel.objects.get(id = speakdata.user_id)
        print("》》《》《》",user)
        busin = ShopsModel.objects.get(id = speakdata.shops_id)
        print("MMMMMMMMM",busin)

        datchat = Speaking.objects.filter(chatid=chatid).all()
        for i in datchat:
            dats = {
                'chatid':chatid,
                'uname':user.nickname,
                'uid':user.id,
                'bid':busin.id,
                'bname':busin.name,
                'msg':i.speatxt,
                'status':i.reply,
            }
            data.append(dats)
        return Response({'code':200,'msg':'ok','data':data})

# 根据商家ID去获取房间号再获取用户ID
class Broomuser(APIView):
    def get(self,request):
        data = []
        bid = request.query_params.get('shops_id')
        userid = Speaking.objects.filter(Q(bid_id=bid)).values("user_id").distinct()
        for i in userid:
            data.append(i)
        for j in data:
            user = UserModel.objects.get(id = j.get('user_id'))
            j['uname'] = user.nickname
        return Response({'code':200,'mag':'ok','data':data})

